package com.google.gson.internal;

import com.google.gson.InstanceCreator;
import com.google.gson.a.a;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public final class f {
    private final i a;

    public f() {
        this(new i());
    }

    public f(i iVar) {
        this.a = iVar;
    }

    private ObjectConstructor a(Class cls) {
        try {
            final Constructor declaredConstructor = cls.getDeclaredConstructor(new Class[0]);
            if (!declaredConstructor.isAccessible()) {
                declaredConstructor.setAccessible(true);
            }
            return new ObjectConstructor(this) {
                final /* synthetic */ f b;

                public Object construct() {
                    try {
                        return declaredConstructor.newInstance(null);
                    } catch (Throwable e) {
                        throw new RuntimeException("Failed to invoke " + declaredConstructor + " with no args", e);
                    } catch (InvocationTargetException e2) {
                        throw new RuntimeException("Failed to invoke " + declaredConstructor + " with no args", e2.getTargetException());
                    } catch (IllegalAccessException e3) {
                        throw new AssertionError(e3);
                    }
                }
            };
        } catch (NoSuchMethodException e) {
            return null;
        }
    }

    private ObjectConstructor a(final Type type, final Class cls) {
        return new ObjectConstructor(this) {
            final /* synthetic */ f c;
            private final n d = n.a();

            public Object construct() {
                try {
                    return this.d.a(cls);
                } catch (Throwable e) {
                    throw new RuntimeException("Unable to invoke no-args constructor for " + type + ". " + "Register an InstanceCreator with Gson for this type may fix this problem.", e);
                }
            }
        };
    }

    private ObjectConstructor b(Class cls) {
        return Collection.class.isAssignableFrom(cls) ? SortedSet.class.isAssignableFrom(cls) ? new ObjectConstructor(this) {
            final /* synthetic */ f a;

            {
                this.a = r1;
            }

            public Object construct() {
                return new TreeSet();
            }
        } : Set.class.isAssignableFrom(cls) ? new ObjectConstructor(this) {
            final /* synthetic */ f a;

            {
                this.a = r1;
            }

            public Object construct() {
                return new LinkedHashSet();
            }
        } : Queue.class.isAssignableFrom(cls) ? new ObjectConstructor(this) {
            final /* synthetic */ f a;

            {
                this.a = r1;
            }

            public Object construct() {
                return new LinkedList();
            }
        } : new ObjectConstructor(this) {
            final /* synthetic */ f a;

            {
                this.a = r1;
            }

            public Object construct() {
                return new ArrayList();
            }
        } : Map.class.isAssignableFrom(cls) ? new ObjectConstructor(this) {
            final /* synthetic */ f a;

            {
                this.a = r1;
            }

            public Object construct() {
                return new LinkedHashMap();
            }
        } : null;
    }

    public ObjectConstructor a(a aVar) {
        final Type b = aVar.b();
        Class a = aVar.a();
        final InstanceCreator instanceCreator = (InstanceCreator) this.a.a(b, false);
        if (instanceCreator != null) {
            return new ObjectConstructor(this) {
                final /* synthetic */ f c;

                public Object construct() {
                    return instanceCreator.createInstance(b);
                }
            };
        }
        ObjectConstructor a2 = a(a);
        if (a2 != null) {
            return a2;
        }
        a2 = b(a);
        return a2 == null ? a(b, a) : a2;
    }

    public String toString() {
        return this.a.toString();
    }
}
